package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.MeManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.DriverInfo;
import com.sgb.kjwl.model.entity.GoodsOrderEntity;
import com.sgb.kjwl.model.entity.self.GoodsSourceDetail;
import com.sgb.kjwl.model.entity.self.UserAccount;
import com.sgb.kjwl.model.entity.shopping.PayCardEntity;
import com.sgb.kjwl.model.entity.transport.CompanyGoodsOrderEntity;
import com.sgb.kjwl.model.entity.transport.CompanyInvoiceEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourcePlace;
import com.sgb.kjwl.model.entity.transport.PayInfoEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.model.entity.transport.SingleItemEntity;
import com.sgb.kjwl.utils.EntityConversion;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.activity.transport.PayGoodsOrderActivity;
import com.sgb.link.entity.AMapAddressEntity;
import com.sgb.link.utils.AddressHelper;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.util.UserOperating;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/2/7
 * @Description: 接单viewModel
 */
public class GroupPayGoodsViewModel extends BaseViewModel {

    public MutableLiveData<List<CarInfo>> ldCar = new MutableLiveData<>();
    public MutableLiveData<List<CarInfo>> ldCarinfo = new MutableLiveData<>();
    public MutableLiveData<AMapAddressEntity> ldLocation = new MutableLiveData<>();
    public MutableLiveData<DriverInfo> ldDriverInfo = new MutableLiveData<>();
    public MutableLiveData<PayInfoEntity> ldTradeNo = new MutableLiveData<>();
    public MutableLiveData<PayInfoEntity> ldUnfreeze = new MutableLiveData<>();
    public MutableLiveData<GoodsSourceDetail> soureDetail = new MutableLiveData<>();
    public MutableLiveData<List<CompanyInvoiceEntity>> ldCompanyInvoice = new MutableLiveData<>();
    GoodsSourceDetail goodsSourcePkace;
    String bankInfo = "";

    public GroupPayGoodsViewModel(@NonNull Application application) {
        super(application);
    }


    /**
     * 获取车辆信息
     */
    public void getCarInfo(Context context,String no) {
        DialogHelper.showProgressDialog(context, "请求中...");
        NewAPIManager.getInstance().getLogisticsAPI().autotrophAndAffiliatedVehicle(no).enqueue(new ICallBack<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CarInfo>> data) {
                ldCar.postValue(data.getData());
            }
        });
    }



    /**
     * 获取定位信息
     */
    public void getCurrentLocation(Context context) {
        AddressHelper.getInstance().getCurrentLocation(context, entity -> {
            if (entity != null) {
                ldLocation.postValue(entity);
            }
        });
    }




    /**
     * 获取货源详情
     */
    public void getGoodsDetail(Context activity, String sourceNo) {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        String isLogin = UserOperating.getInstance().isLogged() ? "true" : "false";
        NewAPIManager.getInstance().getLogisticsAPI().getGoodsSourceDetail(isLogin, sourceNo,null).enqueue(new Callback<BaseEntity<GoodsSourceDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<GoodsSourceDetail>> call, Response<BaseEntity<GoodsSourceDetail>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    soureDetail.postValue(response.body().getData());
                } else if (response.body().getState().equals("fail")) {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<GoodsSourceDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "操作失败");
            }
        });
    }

    /**
     * 接单
     */
    public void quotation(Context context, CompanyGoodsOrderEntity entity) {
        DialogHelper.showProgressDialog(context, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        //todo 换接口
        NewAPIManager.getInstance().getLogisticsAPI().payOrder(entity).enqueue(new ICallBack<BaseEntity<PayInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PayInfoEntity> data) {
                if(data.isSuccess()){
                    ldTradeNo.postValue(data.getData());
                }
            }
        });
    }


    public void getDriverInfo() {
        NewAPIManager.getInstance().getLogisticsAPI().getDriverInfo().enqueue(new Callback<BaseEntity<DriverInfo>>() {
            @Override
            public void onResponse(Call<BaseEntity<DriverInfo>> call, Response<BaseEntity<DriverInfo>> response) {
                if (response.body() == null || response.body().getState() == null) return;

                if (response.body().getState().equals("ok")) {
                    ldDriverInfo.setValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<DriverInfo>> call, Throwable t) {
            }
        });
    }

    public void isFirstPay(PayGoodsOrderActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().isFirstPay().enqueue(new Callback<BaseEntity<UserAccount>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserAccount>> call, Response<BaseEntity<UserAccount>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals("ok")) {
                    if (response.body().getData().isFirstPay()) {
                        activity.showTextDialog(true);
                    } else {
                        activity.toPayOrder();
                    }
                } else {
                    MToast.showToast(activity, TextUtils.isEmpty(response.body().getMsg()) ? "fail" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserAccount>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getQuickBankList(Context activity) {

        MeManager.getInstance().getMaterialAPI().getQuick().enqueue(new Callback<BaseEntity<List<PayCardEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PayCardEntity>>> call, Response<BaseEntity<List<PayCardEntity>>> response) {


                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        bankInfo = response.body().getData().get(0).openingBank;
                    } else {
                        bankInfo = "";
                    }

                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PayCardEntity>>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    /**
     * 取消支付立即解冻当前货源
     * @param goodsSourceNo
     * @param orderNo
     */
    public void unfreeze(Context activity,String goodsSourceNo, String orderNo) {
        //showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        map.put("goodsSourceNo", goodsSourceNo);
        NewAPIManager.getInstance().getLogisticsAPI().unfreeze(map).enqueue(new Callback<BaseEntity<PayInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PayInfoEntity>> call, Response<BaseEntity<PayInfoEntity>> response) {
                // showContentView();
                if (response.body()==null)return;
                if (response.body().getState().equals("ok")) {
                    ldUnfreeze.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PayInfoEntity>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 获取公司发票列表
     */
    public void getInvoiceList(Context context) {
        //判断公司或者个人调用不同的发票接口
        DialogHelper.showProgressDialog(context, "请求中...");
        if (UserOperating.getInstance().isCompany()) {
            NewAPIManager.getInstance().getLogisticsAPI().getCompanyInvoiceList().enqueue(invoiceCallBack());
        } else {
            NewAPIManager.getInstance().getLogisticsAPI().getPersonInvoiceList().enqueue(invoiceCallBack());
        }
    }

    @NotNull
    private ICallBack<BaseEntity<List<CompanyInvoiceEntity>>> invoiceCallBack() {
        return new ICallBack<BaseEntity<List<CompanyInvoiceEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyInvoiceEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() != null) {
                    ldCompanyInvoice.postValue(data.getData());
                }
            }
        };
    }
}
